<%@ page import="java.text.DecimalFormat" %>
<%@ page import="com.example.cdpbookstore.pojo.entity.CartItem" %>
<%@ page import="java.util.List" %>
<%@ page import="java.math.BigDecimal" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    DecimalFormat dcf = new DecimalFormat("0.00");
    List<CartItem> cart = (List<CartItem>) session.getAttribute("cart");
    // 计算总金额
    BigDecimal total = BigDecimal.ZERO;
    if (cart != null) {
        for (CartItem cartItem : cart) {
            total = total.add(cartItem.getTotal()); //累加汇总
        }
    }
    pageContext.setAttribute("total", dcf.format(total));
%>
<!DOCTYPE html>
<html lang="zh-Hans-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>布克书店 - 购物车</title>

    <link rel="icon" type="image/png" href="image/web-icon.png">
    <link rel="stylesheet" type="text/css" href="lib/font-awesome-6.5.2/css/all.css">
    <link rel="stylesheet" type="text/css" href="css/common/com-a.css">
    <link rel="stylesheet" type="text/css" href="css/common/com-b.css">
    <link rel="stylesheet" type="text/css" href="css/shopping-cart.css">
</head>

<body>
<div class="main">
    <jsp:include page="common/header1.jsp"/>

    <!-- shopping cart 购物车部分 -->
    <div class="shoppingCart">
        <div class="sc-center">
            <div class="total-warp">
                <h1 class="total">总金额：￥${total}</h1><a href="cart-check-out" class="btn checkout">结账</a>
            </div>
            <div class="list-warp">
                <%--                <jsp:useBean id="cart" scope="request" type="java.util.List<com.example.cdpbookstore.pojo.entity.CartItem>"/>--%>
                <c:forEach var="cartItem" items="${cart}">
                    <div class="item">
                        <div><img class="cover" src="/img/book/${cartItem.book.images}" alt=""></div>
                        <div>${cartItem.book.bookName}</div>
                        <div>￥${cartItem.book.currentPrice}</div>
                        <div class="amount-warp">
                            <a href="quantity-inc-dec?action=dec&id=${cartItem.book.id}" class="btn-num">
                                <i class="fa fa-regular fa-square-minus"></i>
                            </a>
                            <input type="text" name="" class="amount" value="${cartItem.quantity}" readonly>
                            <a href="quantity-inc-dec?action=inc&id=${cartItem.book.id}" class="btn-num">
                                <i class="fa fa-regular fa-square-plus"></i>
                            </a>
                        </div>
                        <div><a href="remove-from-cart?id=${cartItem.book.id}" class="btn delete">删除</a></div>
                    </div>
                </c:forEach>
            </div>
        </div>
    </div>

    <jsp:include page="common/footer1.jsp"/>
</div>
</body>

</html>
